package com.amit.struts;
import java.util.List;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;



import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;


public class ContactAction extends JLCBaseDispatchAction{
	
	int tor=5;
	int start=0;
	int tnor=0;
	int end=0;
	
	public ActionForward addContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("addContact()-ContactAction");
		
		ContactForm cf=(ContactForm) form;
		
		ContactTO cto=new ContactTO(cf.getFname(), cf.getLname(), cf.getEmail(), cf.getPhone(), "Enabled");
		
		int x=contactService.addContact(cto);
		
		System.out.println(x);

		return mapping.findForward("addcontactstatus");
	}
	
	
	public ActionForward searchContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("searchContact()-ContactAction");
		
		request.getSession().removeAttribute("EDIT");
		
		ContactForm cf=(ContactForm) form;
		
		ContactTO cto=contactService.getContactByEmail(cf.getEmail());
		
		if(cto==null)
		{
			request.setAttribute("NOT_FOUND", "No Record found with email"+" "+cf.getEmail());
			
			return mapping.findForward("notfound");
		}
		
		else
		{
			cf.setContactId(cto.getContactId());
			cf.setFname(cto.getFname());
			cf.setLname(cto.getLname());
			cf.setPhone(cto.getPhone());
			cf.setStatus(cto.getStatus());
		}
	

		return mapping.findForward("updatecontact");
	}
	
	
	

	public ActionForward updateContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("updateContact()-ContactAction");
		
		request.getSession().removeAttribute("CID");
		ContactForm cf=(ContactForm) form;
		

		System.out.println(cf.getContactId());
		System.out.println(cf.getFname());
		System.out.println(cf.getLname());
		System.out.println(cf.getEmail());
		System.out.println(cf.getPhone());
		System.out.println(cf.getStatus());
		
		ContactTO cto=new ContactTO(cf.getContactId(), cf.getFname(),
				cf.getLname(), cf.getEmail(), cf.getPhone(), cf.getStatus());
		
		int x=contactService.updateContact(cto);
	
		System.out.println(x);
		
		HttpSession session=request.getSession();
		Object obj=session.getAttribute("EDIT");
		
		if(obj!=null)
		{
			session.setAttribute("EDIT", "FALSE");
			
			List<ContactTO> clist=contactService.getAllContacts(start,tor);
			session.setAttribute("CLIST", clist);
			setNextPrevious(request);
			
			return mapping.findForward("showallcontactshome");
		}
		
		else
		{
			return mapping.findForward("updatecontactstatus");
		}
		
	}


	private void setNextPrevious(HttpServletRequest request) {
		
		if(start+1>5)
		{
			request.setAttribute("SHOW_PREVIOUS", "OK");
		}
		if(end<tnor)
		{
			request.setAttribute("SHOW_NEXT", "OK");
		}
		
		
	}
	
	

	public ActionForward showAllContacts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("showAllContact()-ContactAction");
		
		request.getSession().removeAttribute("CID");
		request.getSession().setAttribute("EDIT", "False");
		
		start=0;
		
		tnor=contactService.getTotalNumberOfRecords();
		List<ContactTO> clist=contactService.getAllContacts(start, tor);
		
		if(tnor<1)
		{
			request.setAttribute("NOT_FOUND", "No Records available");
		}
		
		else
		{
			end=tnor;
			
			if(end>tor)
				end=tor;
			
			HttpSession session=request.getSession();
			
			session.setAttribute("CLIST", clist);
			session.setAttribute("EDIT","FALSE");
			session.setAttribute("BEGIN",start+1);
			session.setAttribute("END",end);
			session.setAttribute("TNOR",tnor);
			
			if(tnor>end)
			{
				request.setAttribute("SHOW_NEXT", "OK");
			}
			

		}
		
       return mapping.findForward("showallcontactshome");
		
	}

	
	public ActionForward showNextContacts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("showNextContact()-ContactAction");
		
		request.getSession().removeAttribute("CID");
		request.getSession().setAttribute("EDIT", "FALSE");
		
		HttpSession session=request.getSession();
		
		Integer i1=(Integer) session.getAttribute("END");
		
		start=i1.intValue();
		
		tnor=contactService.getTotalNumberOfRecords();
		List<ContactTO> clist=contactService.getAllContacts(start, tor);
		
		if(tnor<1)
		{
			request.setAttribute("NOT_FOUND", "No Records available");
		}
		
		else
		{
			end=start+tor;
			
			if(end>tnor)
			{
				end=tnor;
			}
			
			session.setAttribute("CLIST", clist);
			session.setAttribute("BEGIN",start+1);
			session.setAttribute("END",end);
			session.setAttribute("TNOR",tnor);
			
			setNextPrevious(request);
			
		}
		
return mapping.findForward("showallcontactshome");
		
	}

	

	public ActionForward showPreviousContacts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("showPreviousContact()-ContactAction");
		
		request.getSession().removeAttribute("CID");
		request.getSession().setAttribute("EDIT", "FALSE");
		
		HttpSession session=request.getSession();
		
		Integer i1=(Integer) session.getAttribute("BEGIN");
		
		start=i1.intValue()-tor-1;
		
		tnor=contactService.getTotalNumberOfRecords();
		List<ContactTO> clist=contactService.getAllContacts(start, tor);
		
		if(tnor<1)
		{
			request.setAttribute("NOT_FOUND", "No Records available");
		}
		
		else
		{
			end=start+tor;
			
			if(end>tnor)
			{
				end=tnor;
			}
			
			session.setAttribute("CLIST", clist);
			session.setAttribute("BEGIN",start+1);
			session.setAttribute("END",end);
			session.setAttribute("TNOR",tnor);
			
			setNextPrevious(request);
			
		}
		
return mapping.findForward("showallcontactshome");
		
	}
	
	
	public ActionForward editContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("editContact()-ContactAction");
		
		DynaActionForm daf=(DynaActionForm) form;
		
		String cid=daf.get("cid").toString();
			
		HttpSession session=request.getSession();
		
			session.setAttribute("EDIT", "TRUE");
			session.setAttribute("CID",cid);
			setNextPrevious(request);
			
        return mapping.findForward("showallcontactshome");
		
	}
	
	
	

	public ActionForward deleteContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("deleteContact()-ContactAction");
		
		request.getSession().removeAttribute("CID");
		request.getSession().setAttribute("EDIT", "FALSE");
		
		DynaActionForm daf=(DynaActionForm) form;
		
		String cid=daf.get("cid").toString();
		
		int x=contactService.deleteContact(cid);
		
		System.out.println(x);
		
		if(x>0)
		{
			request.setAttribute("DELETED", "Record deleted Successfully");
		}
		tnor=contactService.getTotalNumberOfRecords();
		if(tnor<1)
		{
		request.setAttribute("NOT_FOUND", "No Record available");
		}
		
		else
		{
			Integer i1=(Integer) request.getSession().getAttribute("BEGIN");
			
			start=i1.intValue();
			
			if(tnor<start)
			{
				start=start-tor;
			}
			
			end=start+tor-1;
			
			if(end>tnor)
			{
				end=tnor;
			}
			
			
			List<ContactTO> clist=contactService.getAllContacts(start-1, tor);
			
			request.getSession().setAttribute("CLIST", clist);
			request.getSession().setAttribute("BEGIN", start);
			request.getSession().setAttribute("END", end);
			request.getSession().setAttribute("TNOR", tnor);
			
			setNextPrevious(request);
			
		}
	
        return mapping.findForward("showallcontactshome");
		
	}
}
